package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.controller.dto.OrderAndOrderItemDto;
import cn.cfoodc.sharding.controller.dto.OrderBingdingOrderItemDto;
import cn.cfoodc.sharding.domain.Order;
import cn.cfoodc.sharding.domain.TOrderItem;
import cn.cfoodc.sharding.service.OrderService;
import cn.cfoodc.sharding.service.TOrderItemService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;


@RestController
public class DemoOrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private TOrderItemService tOrderItemService;

    @Autowired
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;

    //使用order_id按照配置全库全表查询
    @GetMapping("/getOrderAndAgrs")
    public Order getOne(@RequestParam("orderId") Long orderId){
        return orderService.selectByPrimaryKey(orderId);
    }


    /**
     *  测试增加一个订单多个订单详情
     * @return
     */
    @PostMapping("/insertOrderAndArgs")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE，这里只引入了XA
    public int insert4() throws InterruptedException {
        //每个用户增加10笔订单
        for (int i = 0; i < 11; i++) {
            Long user_id = (Long) NEXT_ID.generateKey();
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            Long order_id = (Long) NEXT_ID.generateKey();
            Order t = new Order();
            t.setCreateTime(ts);
            t.setOrderId(order_id);
            t.setOrderNo(order_id+"");
            t.setUserId(user_id);
            orderService.insert(t);
            //一笔订单增加11笔订单详情
            for (int j = 0; j < 12; j++) {
                TOrderItem ti = new TOrderItem();
                ti.setCreateDatetime(ts);
                ti.setName(UUID.randomUUID().toString());
                ti.setOrderId(order_id);
                ti.setOrderItemId((Long) NEXT_ID.generateKey());
                ti.setUserId(user_id);
                tOrderItemService.insert(ti);
            }
        }

        return 1;
    }

    //单表查询
    @GetMapping("/getOneOrderAndOrderDetaiAndArgs_1")
    public Order getPage(@RequestParam("userId") Long userId , @RequestParam("orderId") Long orderId){
        return orderService.selectOneByUseridAndOrderid(userId,orderId);
    }

    //连表查询，左连接子查询
    @GetMapping("/getOrderBingdingOrderItemAndArgs_1")
    public List<OrderBingdingOrderItemDto> getOrderBingdingOrderItem(@RequestParam("userId") Long userId , @RequestParam("orderId") Long orderId){
        return orderService.selectAllOrderBingdingOrderItem(userId , orderId);
    }



    //连表查询，测试笛卡尔积
    @GetMapping("/getOrderBingdingOrderItem2")
    public List<OrderBingdingOrderItemDto> getOrderBingdingOrderItem2(@RequestParam("userId") Long userId,@RequestParam("userId2") Long userId2) throws InterruptedException {
        return orderService.selectAllOrderBingdingOrderItem2( userId , userId2 );
    }

}
